home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_5.2 / linerid / linerid.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  4.0 KB  |  152 lines

  1. /*
  2.  * linerid.c
  3.  *
  4.  * Practical Algorithms for Image Analysis
  5.  *
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9.  
  10. /* LINERID:     program takes input PCC, removes lines below minimum
  11.  *            tolerance, and writes output PCC
  12.  *           usage: linerid infile outfile [-e MINLINE_EE] [-f MINLINE_FE] [-L]
  13.  *
  14.  */
  15.  
  16. #define MINLINE_EE 30           /* end to endline minimum length */
  17. #define MINLINE_FE 10           /* end to/from feature minimum length */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include "pcc2.h"               /* for PCC programs */
  23. extern void print_sos_lic ();
  24.  
  25. unsigned char *fcCode;          /* code storage */
  26. long nByteCode;                 /* no. bytes in code storage */
  27.  
  28. int input (int, char **, long *, long *);
  29. int usage (short);
  30.  
  31. int argc;
  32. char *argv[];
  33. long *minEE, *minFE;
  34.  
  35.  
  36. main (argc, argv)
  37.      int argc;
  38.      char *argv[];
  39. {
  40.   long width, height;           /* image size */
  41.   struct attributes *attr;      /* level 1 attributes */
  42.   long nAttr;                   /* no. of line structures in attr. array */
  43.   long minEE, minFE;            /* minimum EE and FE line thresholds */
  44.  
  45. /* user input */
  46.   if ((input (argc, argv, &minEE, &minFE)) < 0)
  47.     return (-1);
  48.   minEE *= 10;                  /* length units = [pixels * 10 ] */
  49.   minFE *= 10;
  50.  
  51. /* open input PCC file */
  52.   if (pccread (argv[1], &fcCode, &nByteCode, &width, &height) == -1)
  53.     exit (1);
  54.   printf ("image size: %dx%d, PCC length = %d\n", width, height, nByteCode);
  55.  
  56. /* construct tables of PCC decodes */
  57.   pccdecodes ();
  58.  
  59. /* construct TLC level 1 array of attributes */
  60.   tlc1attr (&attr, &nAttr);
  61.   printf ("number of line features = %d\n", nAttr);
  62.  
  63. /* remove PCC code for lines below minimum length tolerance */
  64.   tlc1tag (attr, nAttr, minEE, minFE);
  65.   printf ("number of PCC bytes before filtering = %4d\n", nByteCode);
  66.   nByteCode = tlc1rid (attr, nAttr);
  67.   printf ("number of PCC bytes after filtering  = %4d\n", nByteCode);
  68.  
  69. /* write out PCC file */
  70.   pccwrite (argv[2], fcCode, nByteCode, width, height);
  71.  
  72.   return (0);
  73. }
  74.  
  75.  
  76. /* USAGE:       function gives instructions on usage of program
  77.  *                    usage: usage (flag)
  78.  *              When flag is 1, the long message is given, 0 gives short.
  79.  */
  80.  
  81. int
  82. usage (flag)
  83.      short flag;                /* flag =1 for long message; =0 for short message */
  84. {
  85.  
  86. /* print short usage message or long */
  87.   printf ("USAGE: linerid infile outfile [-e MINLINE_EE] [-f MINLINE_FE] [-L]\n");
  88.   if (flag == 0)
  89.     return (-1);
  90.  
  91.   printf ("\nlinerid filters out small lines of two types:\n");
  92.   printf ("    EE lines are isolated endline-to-endline segments;\n");
  93.   printf ("    FE lines are attached feature-to-endline segments.\n\n");
  94.   printf ("ARGUMENTS:\n");
  95.   printf ("    infile: input filename (PCC)\n");
  96.   printf ("   outfile: output filename (PCC)\n\n");
  97.   printf ("OPTIONS:\n");
  98.   printf ("  -e MINLINE_EE: minimum length of end-end, isolated lines;");
  99.   printf (" (default=%d)\n", MINLINE_EE);
  100.   printf ("  -f MINLINE_FE: minimum length of feature-end, attached lines;");
  101.   printf (" (default=%d)\n", MINLINE_FE);
  102.   printf ("             -L: print Software License for this module\n");
  103.  
  104.  
  105.   return (-1);
  106. }
  107.  
  108.  
  109. /* INPUT:       function reads input parameters
  110.  *                  usage: input (argc, argv, &minEE, &minFE)
  111.  */
  112.  
  113. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  114.  
  115. int
  116. input (argc, argv, minEE, minFE)
  117.      int argc;
  118.      char *argv[];
  119.      long *minEE, *minFE;       /* minimum length of EE and FE line segments */
  120. {
  121.   long n;
  122.  
  123.   if (argc == 1)
  124.     USAGE_EXIT (1);
  125.   if (argc == 2)
  126.     USAGE_EXIT (0);
  127.  
  128.   *minEE = MINLINE_EE;
  129.   *minFE = MINLINE_FE;
  130.  
  131.   for (n = 3; n < argc; n++) {
  132.     if (strcmp (argv[n], "-e") == 0) {
  133.       if (++n == argc)
  134.         USAGE_EXIT (0);
  135.       *minEE = (long) atol (argv[n]);
  136.     }
  137.     else if (strcmp (argv[n], "-f") == 0) {
  138.       if (++n == argc)
  139.         USAGE_EXIT (0);
  140.       *minFE = atol (argv[n]);
  141.     }
  142.     else if (strcmp (argv[n], "-L") == 0) {
  143.       print_sos_lic ();
  144.       exit (0);
  145.     }
  146.     else
  147.       USAGE_EXIT (0);
  148.   }
  149.  
  150.   return (0);
  151. }
  152.